package com.example.administrator.xinyutg.task;

import android.os.AsyncTask;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.example.administrator.xinyutg.adapter.DataAdapter;
import com.example.administrator.xinyutg.bean.AppEntity;
import com.example.administrator.xinyutg.bean.GroupData;
import com.example.administrator.xinyutg.utils.HttpUtil;

import java.util.List;

/**
 * Created by Administrator on 2015/9/26.
 */
public class DataDownTask extends AsyncTask<String, Void, List<GroupData>> {
    private List<GroupData> dataList;
    private DataAdapter adapter;

    public DataDownTask(List<GroupData> dataList, DataAdapter adapter) {
        this.dataList = dataList;
        this.adapter = adapter;
    }

    @Override
    protected List<GroupData> doInBackground(String... params) {
        List<GroupData> temp = null;
        String result = HttpUtil.getJSON(params[0]);
        if (!TextUtils.isEmpty(result)) {
            AppEntity entity = JSONObject.parseObject(result, AppEntity.class);
            if (entity != null && entity.getResult() != null) {
                temp = entity.getResult().getData();
            }

        }

        return temp;
    }

    @Override
    protected void onPostExecute(List<GroupData> groupDatas) {
        super.onPostExecute(groupDatas);
        if (groupDatas != null && !groupDatas.isEmpty()) {
            dataList.addAll(groupDatas);
            adapter.notifyDataSetChanged();
        }
    }

}
